package com.atguigu.spzx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.spzx.mapper.SysRoleUserMapper;
import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.service.SysRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class SysRoleUserServiceImpl implements SysRoleUserService {


    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public void doAssign(AssginRoleDto assginRoleDto) {
        List<Long> roleIdList = assginRoleDto.getRoleIdList();
        Long userId = assginRoleDto.getUserId();

        // 删除之前的所有的用户所对应的角色数据
        sysRoleUserMapper.deleteByUserId(userId);

        // 分配新的角色数据
        if (!CollectionUtil.isEmpty(roleIdList)) {
            for (Long roleId : roleIdList) {
                sysRoleUserMapper.doAssign(userId, roleId);
            }
        }


    }

}
